package com.mf.morefun.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mf.morefun.R;
import com.mf.morefun.bean.GrindingEarsBean;
import com.mf.morefun.listener.ReadDetailTypeListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GrindingEarsAdapter extends BaseAdapter {
    private Context mContext;
    private List<GrindingEarsBean> mList;
    private int selectIndex = 0;
    private ReadDetailTypeListener listener;

    public int getSelectIndex() {
        return selectIndex;
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public ReadDetailTypeListener getListener() {
        return listener;
    }

    public void setListener(ReadDetailTypeListener listener) {
        this.listener = listener;
    }

    public GrindingEarsAdapter(Context mContext, List<GrindingEarsBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.layout_earsitem, null);
        } else {
            view = convertView;
        }
        if (null != mList && mList.size() > 0){
            ViewHolder holder = new ViewHolder(view);
            holder.tvTitle.setText(mList.get(position).getName());
            if (position == 0){
                if (getSelectIndex() == position){
                    holder.rlBg.setBackgroundResource(R.drawable.blue_topbgshape);
                    holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.white));
                }else {
                    holder.tvTitle.setTextColor(0xff666666);
                    holder.rlBg.setBackgroundResource(R.drawable.white_topshape);
                }
            }else if(position == (mList.size() - 1)){
                if (getSelectIndex() == position){
                    holder.rlBg.setBackgroundResource(R.drawable.blue_bottombgshape);
                    holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.white));
                }else {
                    holder.tvTitle.setTextColor(0xff666666);
                    holder.rlBg.setBackgroundResource(R.drawable.white_bottomshape);
                }
            }else {
                if (getSelectIndex() == position){
                    holder.rlBg.setBackgroundResource(R.color.main_default_color);
                    holder.tvTitle.setTextColor(mContext.getResources().getColor(R.color.white));
                }else {
                    holder.tvTitle.setTextColor(0xff666666);
                    holder.rlBg.setBackgroundResource(R.color.white);
                }
            }
            holder.ivDelete.setTag(position);
            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.callBack((int) v.getTag());
                }
            });
        }
        return view;
    }

    static class ViewHolder {
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.iv_delete)
        ImageView ivDelete;
        @BindView(R.id.rl_bg)
        RelativeLayout rlBg;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
